<?php

namespace App\Kuafu\CustomerCoupon\Resources;

use App\Kuafu\Activity\Enum\ActivityEnum;
use App\Kuafu\Config\Model\ConfigModel;
use App\Kuafu\Coupon\Service\CouponService;
use App\Kuafu\Customer\Enum\PlatformCodeEnum;
use App\Kuafu\CustomerCoupon\Enum\CodeEnum;
use Illuminate\Http\Resources\Json\JsonResource;

class CustomerCouponDetailResource extends JsonResource
{
    public function toArray($request): array
    {
        list($customerStatus, $customerStatusText) = $this->getCustomerStatus();
        return [
            "id"                 => $this->id,
            "couponType"         => $this->couponType,
            "couponTypeText"     => $this->getCouponTypeText(),
            "discount"           => $this->discount,
            "discountUnit"       => $this->getDiscountUnit(),
            "userId"             => $this->userId,
            "activityId"         => $this->activityId,
            "couponId"           => $this->couponId,
            "couponCode"         => $this->couponCode,
            "name"               => $this->name,
            "couponImg"          => $this->getCustomerCouponImg(),
            "desc"               => $this->desc,
            "startAt"            => $this->startAt,
            "startAtDate"        => formatDateYmd($this->startAt),
            "endAtDate"          => formatDateYmd($this->endAt),
            "endAt"              => $this->endAt,
            "useCondition"       => $this->useCondition,
            "useConditionText"   => $this->getUseConditionText(),
            "status"             => $this->status,
            "statusText"         => $this->getStatusText(),
            "customerStatus"     => $customerStatus,
            "customerStatusText" => $customerStatusText,
            "suitShop"           => $this->getSuitShop($this->platformId ?? 0),

            // coupon
            'availableSource'     => $this->couponRel->availableSource,
            'availableSourceText' => CouponService::availableSourceRules($this->couponRel->availableSource),
            'coupon_shop_rel'     => $this->couponShopRel,
            'useTimeType'         => $this->useTimeType



        ]; // TODO: Change the autogenerated stub
    }

    public function getCustomerStatus()
    {
        if ($this->status > 0) {
            return [3, "已使用"];
        } else {
            if (time() < $this->startAt) {
                return [2, "待生效"];
            }

            if (time() > $this->startAt && time() < $this->endAt) {
                return [1, "待使用"];
            }

            if (time() > $this->endAt) {
                return [0, "已过期"];
            }
        }
    }


    /**
     * 获取优惠券默认图
     *
     * @return mixed|string
     */
    public static function getCustomerCouponDefaultImg()
    {
        return ConfigModel::query()
                          ->where("key", "customerCouponDefaultImg")
                          ->where("type", "customerCoupon")
                          ->pluck("value")->first() ?? "";
    }

    /**
     * 获取优惠券图片
     *
     * @return mixed|string
     */
    public function getCustomerCouponImg()
    {
        return $this->couponImg ? picturePath($this->couponImg) : self::getCustomerCouponDefaultImg();
    }


    /**
     * 获取券面值单位
     *
     * @return string
     */
    public function getDiscountUnit(): string
    {
        $unit = "";
        switch ($this->couponType) {
            case CodeEnum::COUPON_TYPE_EXCHANGE:
            case CodeEnum::COUPON_TYPE_CASH:
                $unit = "元";
                break;
            case CodeEnum::COUPON_TYPE_DISCOUNT:
                $unit = "折";
                break;
        }
        return $unit;
    }


    /**
     * 获取优惠券使用状态文案
     *
     * @return string
     */
    public function getStatusText(): string
    {
        return CodeEnum::COUPON_STATUS_TEXT[$this->status] ?? "-";
    }


    /**
     * 优惠券类型文案获取
     *
     * @return string
     */
    public function getCouponTypeText(): string
    {
        return CodeEnum::COUPON_TYPE_TEXT[$this->couponType] ?? "-";
    }

    /**
     * 获取优惠券使用门槛文案
     *
     * @return string
     */
    public function getUseConditionText(): string
    {
        if ($this->useCondition == 0) {
            return "无门槛";
        } else {
            return sprintf("满%d可用", bcdiv($this->useCondition, 100, 2));
        }
    }

    public function getSuitShop(int $platformId = 0)
    {
        switch ($this->shopType) {
            case ActivityEnum::SHOP_TYPE_ALL:
            case ActivityEnum::SHOP_TYPE_FRANCHISE:
            case ActivityEnum::SHOP_TYPE_DIRECT:
                return sprintf("参与活动%s", ActivityEnum::SHOP_TYPES[$this->shopType] ?? "-");
                break;
            case ActivityEnum::SHOP_TYPE_SELECTION:
                if ($this->couponShopRel->count() > 2) {
                    if ($platformId == PlatformCodeEnum::PLATFORM_APPLET_WECHAT_APPLET) {// 微信小程序取简称
                        return $this->couponShopRel->splice(0, 2)->implode('shopRel.shortName', '、') . '等';
                    } else {
                        return $this->couponShopRel->splice(0, 2)->implode('shopRel.name', '、') . '等';
                    }
                } else {
                    if ($platformId == PlatformCodeEnum::PLATFORM_APPLET_WECHAT_APPLET) {// 微信小程序取简称
                        return $this->couponShopRel->implode('shopRel.shortName', '、');
                    } else {
                        return $this->couponShopRel->implode('shopRel.name', '、');
                    }
                }
                break;
            default:
                return "-";
                break;
        }
    }
}
